const pool = require('../config/db');

// 查询用户及密码
async function getUserByUsername(username) {
  const [rows] = await pool.query('SELECT * FROM users WHERE username = ?', [username]);
  return rows[0];
}

// 查询用户角色
async function getUserRoles(userId) {
  const [rows] = await pool.query(
    `SELECT r.role_name FROM roles r
     JOIN user_roles ur ON r.role_id = ur.role_id
     WHERE ur.user_id = ?`, [userId]);
  return rows.map(r => r.role_name);
}

// 查询用户基本信息
async function getBasicUserInfo(userId) {
  const [rows] = await pool.query(
    'SELECT user_id, username, real_name FROM users WHERE user_id = ?', [userId]);
  return rows[0];
}

module.exports = { getUserByUsername, getUserRoles, getBasicUserInfo };